<%@ page import="com.bookstore.entity.Product" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: LCH
  Date: 2021/9/14
  Time: 16:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    List<Product> products = (List<Product>) request.getAttribute("products");
%>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/favicon.ico">
    <link rel="canonical" href="https://getbootstrap.com/docs/3.4/examples/dashboard/">
    <title>在线书城后台管理系统</title>

    <!-- Bootstrap core CSS -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/dist/css/bootstrap.min.css"
          rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/css/ie10-viewport-bug-workaround.css"
          rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/examples/dashboard/dashboard.css"
          rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/ie8-responsive-file-warning.js"></script>
    <![endif]-->
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false"
                    aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">在线书城后台管理系统</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">设置</a></li>
                <li><a href="#">退出</a></li>
            </ul>
            <form class="navbar-form navbar-right">
                <input type="text" class="form-control" placeholder="搜索">
            </form>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li><a href="index.html">总览 <span class="sr-only">(current)</span></a></li>
                <li class="active"><a href="./product.do?action=query">商品管理</a></li>
                <li><a href="#">订单管理</a></li>
                <li><a href="#">用户管理</a></li>
            </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">


            <h2 class="sub-header">商品列表</h2>
            <!-- 绑定模态框按钮 -->
            <button class="btn btn-primary insert" data-toggle="modal" data-target="#productModal">添加商品</button>
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>商品ID</th>
                        <th>商品名称</th>
                        <th>商品价格</th>
                        <th>商品分类</th>
                        <th>商品数量</th>
                        <th>商品图片</th>
                        <th>商品描述</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <%
                        for (int i = 0; i < products.size(); i++) {
                            Product product = products.get(i);
                    %>
                    <tr>
                        <td><%=product.getId()%></td>
                        <td><%=product.getName()%></td>
                        <td><%=product.getPrice()%></td>
                        <td><%=product.getCategory()%></td>
                        <td><%=product.getPnum()%></td>
                        <td>
                            <img src='/imgUpload/<%=product.getImgurl()%>' style="width: 150px; height: 150px;" alt="..." class="img-thumbnail">
                        </td>
                        <td><%=product.getDescription()%></td>
                        <td>
                            <button class="btn btn-info" data-toggle="modal" data-target="#productModal">修改</button>
                            <button class="btn btn-danger delete" id="delete">删除</button>
                        </td>
                    </tr>
                    <%
                        }
                    %>

                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- 商品模态框部分 -->
<!-- Modal -->
<div class="modal fade" id="productModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">商品添加</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-1"></div>
                    <div class="col-md-10">
                        <form class="form-horizontal" id="product-form" action="./product.do" method="post" enctype="multipart/form-data">
                            <input type="hidden" id="productId"/>
                            <input type="hidden" id="action" value="add"/>
<%--                            <input type="hidden" name="action" value="uploadImg"/>--%>
                            <div class="form-group">
                                <label for="productName" class="col-sm-3 control-label">商品名称</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="productName" placeholder="商品名称">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="productName" class="col-sm-3 control-label">商品价格</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="productPrice" placeholder="商品价格">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="productName" class="col-sm-3 control-label">商品分类</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="productCategory" placeholder="商品分类">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="productName" class="col-sm-3 control-label">商品数量</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="productNum" placeholder="商品数量">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="productName" class="col-sm-3 control-label">商品图片</label>
                                <div class="col-sm-9">
                                    <input type="file" name="file" class="form-control" id="productImg" placeholder="请上传商品图片">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="productName" class="col-sm-3 control-label">商品描述</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" id="productDesc" placeholder="商品描述"></textarea>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="col-md-1"></div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="save">保存</button>
            </div>
        </div>
    </div>
</div>
<script src="../js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/sweetalert/2.1.2/sweetalert.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var messageAlert = function (title, text, icon, button, flag, fun) {
        swal({
            title: title,
            text: text,
            icon: icon,
            button: button,
        }).then(fun);
        return flag;
    };

    $(function () {
        $('#save').click(function () {
            var id = $('#productId').val();
            var name = $('#productName').val();
            var price = $('#productPrice').val();
            var category = $('#productCategory').val();
            var pnum = $('#productNum').val();
            var filename = $('#productImg')[0].files[0].name;
            console.log(filename);
            var description = $('#productDesc').val();
            var action = $('#action').val();

            $.ajax({
                async: true,
                url: "./product.do",
                data: {
                    id: id,
                    action: action,
                    name: name,
                    price: price,
                    category: category,
                    pnum: pnum,
                    imgurl: filename,
                    description: description
                },
                success: function (data) {
                    var text = "添加";
                    if (action === "update") {
                        text = "修改"
                    }
                    if (data == 1) {
                        messageAlert(text + "成功", "商品" + text + "成功！", "success", "确定", true, function (isOk) {
                            $("#product-form")[0].submit();
                            //location.href = "./product.do?action=query";
                        });
                    } else {
                        return messageAlert(text + "失败", "商品" + text + "失败！", "error", "确定", false, null);
                    }
                },
                dataType: "text"
            });
        });
        $('.delete').click(function () {
            var productID = $(this).parents("tr").children("td").first().text();
            console.log(productID)
            messageAlert("删除商品", "是否删除?", "warning", "确定", true, function (isOk) {
                if (isOk) {
                    $.ajax({
                        async: true,
                        url: "./product.do?action=delete",
                        data: {
                            productID: productID,
                        }
                    });
                }
                location.href = "./product.do?action=query";
            });
        });
        $('.btn-info').click(function () {
            var id = $(this).parents("tr").children("td:eq(0)").text();
            var name = $(this).parents("tr").children("td:eq(1)").text();
            var price = $(this).parents("tr").children("td:eq(2)").text();
            var category = $(this).parents("tr").children("td:eq(3)").text();
            var pnum = $(this).parents("tr").children("td:eq(4)").text();
            //var imgurl=$(this).parents("tr").children("td:eq(5)").children("img").attr("src");
            var description = $(this).parents("tr").children("td:eq(6)").text();
            $('#productId').val(id);
            $('#productName').val(name);
            $('#productPrice').val(price);
            $('#productCategory').val(category);
            $('#productNum').val(pnum);
            //$('#productImg').val(imgurl);
            $('#productDesc').text(description);
            $('#myModalLabel').text("商品修改");
            $('#action').val("update");
        });
        $('.insert').click(function () {
            $('#myModalLabel').text("商品添加");
            $('.form-horizontal :input:not(textarea)').val("");
            $('#productDesc').text("");
            $('#action').val("add");
        });
    });
</script>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"
        integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"
        crossorigin="anonymous"></script>
<script>
    window.jQuery || document.write(
        '<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/vendor/jquery.min.js"><\/script>'
    )
</script>
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/dist/js/bootstrap.min.js"></script>
<!-- Just to make our placeholder images work. Don't actually copy the next line! -->
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/vendor/holder.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
